import FPJDefine from "../../Game/FPJ/Prefabs/FPJDefine";
import RoomBtnItem from "./RoomBtnItem/RoomBtnItem";

const { ccclass, property } = cc._decorator;
/** 房间选择按钮 create by tm*/
@ccclass
export default class RoomBtn extends cc.Component {
    //RoomBtnItem 放置区 
    @property(cc.Node)
    node_content: cc.Node = null;
    @property(cc.Prefab)
    btnItem_pfb: cc.Prefab = null;
    //btn item 样式数组  [2, 5, 10, 20, 50, 100]
    @property(cc.SpriteFrame)
    chooseArr: cc.SpriteFrame[] = [];
    @property(cc.SpriteFrame)
    normalArr: cc.SpriteFrame[] = [];
    //变量区
    /** 图片场次信息数组(单位分) */
    private imgArr: number[] = null;
    /** 兑换比例(写死的 一元-->多少分) */
    private theRate: number = null;
    /** 事件 */
    private _event: any = null;
    /** 当前选中room id */
    private roomid: string = null;
    onLoad() {
        this.theRate = 100;
        this.imgArr = [2, 5, 10, 20, 50, 100];
        this.addEvent();
        this.setRoomBtn();
    }
    /** 添加事件 */
    addEvent(): void {
        this.node_content.on(`touchRoomBtnItem`, this.touchBtnItem, this);
        this._event = {};
        this._event[`GameChange`] = cc.vv.eventCustom.on(`GameChange`, this.GameChange.bind(this), this.node);
    }
    /** 移除事件 */
    removeEvent(): void {
        this.node_content.off(`touchRoomBtnItem`, this.touchBtnItem, this);
        if (this._event) {
            for (let _key in this._event) {
                this._event[_key] && cc.vv.eventCustom.off(`${_key}`, this._event[_key]);
                delete this._event[_key];
            }
        }
    }
    /** 点击 btnItem*/
    touchBtnItem(_evt): void {
        this.setChoose(_evt.roomid);
        cc.vv.eventCustom.emit("hallTouchRoomBtn", {
            roomid: _evt.roomid
        });
    }
    /** 游戏变化 */
    GameChange(_evt): void {
        this.setRoomBtn();
    }
    /** 设置某个房间被选中 */
    setChoose(_roomid: string): void {
        this.roomid = _roomid;
        this.node_content.children.forEach(_v => {
            if (_v.active) {
                let _item: RoomBtnItem = _v.getComponent(_v.name);
                _item.setChoose(_item.roomid == _roomid);
            }
        });
    }
    /** 设置room btn 
     * @param 当前选中 
    */
    setRoomBtn(): void {
        if (FPJDefine.room_list && FPJDefine.room_list.length > 0) {
            !FPJDefine.room_list.some(_v => _v.room_id == this.roomid) && (this.roomid = FPJDefine.room_list[0].room_id);
            this.addRoomBtnItem(FPJDefine.room_list.filter(_v => _v[`table_list`] && _v.table_list.length > 0).map(_v => {
                return {
                    roomid: _v.room_id,
                    hallType: 1,
                    roomRate: _v.room_config.room_rate,
                    isChoose: _v.room_id == this.roomid
                }
            }));
        }
    }
    /** 添加房间按钮 */
    addRoomBtnItem(_roomList: {
        /** 房间id */
        roomid: string,//房间id
        /** 大厅类型(1:普通大厅;2:小大厅(多台);) */
        hallType: number,
        /** 兑换比例 */
        roomRate: number,
        /** 是否选中 */
        isChoose: boolean
    }[]): void {
        if (!_roomList) return;
        //排个序
        _roomList.sort((_a, _b) => _b.roomRate - _a.roomRate);
        //设置item
        let _itemw: number = this.itemW();
        _roomList.forEach((_v, _idx) => {
            let _node = this.node_content.children[_idx];
            if (!_node) {
                _node = cc.instantiate(this.btnItem_pfb);
                _node.parent = this.node_content;
            }
            _node.active = true;
            _node.x = (.5 + _idx) * _itemw;
            let _imgIdx: number = this.imgArr.indexOf(Math.floor(this.theRate / _v.roomRate));
            if (_imgIdx >= 0) {
                let _item: RoomBtnItem = _node.getComponent(_node.name);
                _item.initData({
                    roomid: _v.roomid,
                    halltype: _v.hallType,
                    roomSF: [
                        this.normalArr[_imgIdx],
                        this.chooseArr[_imgIdx]
                    ]
                });
                _item.setChoose(_v.isChoose);
            } else {
                _node.destroy();
            }
        });
        //隐藏超出部分
        if (this.node_content.childrenCount > _roomList.length) {
            for (let _idx = _roomList.length; _idx < this.node_content.childrenCount; _idx++) {
                this.node_content.children[_idx].active = false;
            }
        }
        //设置宽高
        this.node_content.width = Math.round(_roomList.length * _itemw);
    }
    /** 获得 room btn item 的宽度 */
    itemW(): number {
        return this.btnItem_pfb.data.width;
    }
    onDestroy() {
        this.removeEvent();
    }
}
